package com.sam.im.heiqun.uis.activities;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.ImGroupEntivity;
import com.sam.im.heiqun.entities.ImMessage;
import com.sam.im.heiqun.entities.UserEntivity;
import com.sam.im.heiqun.uis.adapters.GroupAdapter;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.DividerItemDecorationUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by leijiawen on 2017/5/31.
 */
public class ChoseGroupActivity extends BaseSwipeBackActivity implements GroupAdapter.GroupListClickListener {

    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.pre_tv_title)
    TextView preTvTitle;
    @BindView(R.id.ok)
    TextView ok;
    @BindView(R.id.group_list)
    RecyclerView groupList;
    private GroupAdapter mGroupAdpter;
    private List<ImGroupEntivity> mGroupEntivities = new ArrayList<>();
   private UserEntivity userEntivity;
    private ImMessage getmessage   ;

    int type = 0 ;

    @Override
    protected String getTitleText() {
        type = getIntent().getIntExtra("type",0);
        if (1 == type){
            return getResources().getString(R.string.group);
        }
        return getResources().getString(R.string.select_group);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        if (!EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().register(this);//订阅
        }
        getmessage = (ImMessage) getIntent().getSerializableExtra("msg");
        groupList.setLayoutManager(new LinearLayoutManager(this));
        mGroupAdpter = new GroupAdapter(this);
        mGroupAdpter.setItemClickListener(this);
        groupList.setAdapter(mGroupAdpter);
        userEntivity = ToolsUtils.getUser();
        groupList.addItemDecoration(new DividerItemDecorationUtils(this, LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.black_divider)));
        getLoadeGroup();
    }

    @Override
    public int getContentViewId() {
        return R.layout.layout_chosegroup;
    }
    @OnClick(R.id.pre_v_back)
    public void onViewClicked() {
       finish();
    }

    @Override
    public void onGroupListClick(View view, int position) {
        ImGroupEntivity imGroupEntivity = mGroupEntivities.get(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("groupSeri", imGroupEntivity);
        bundle.putLong("groupid", imGroupEntivity.getId());
       // Log.i("info","这里的消息=="+getmessage.getContent());
        bundle.putSerializable("msg",getmessage);
        EventBus.getDefault().post("关闭");
        EventBus.getDefault().post(111);
        startActivity(ChatGroupActivity.class,bundle);
        finish();
    }
    private void getLoadeGroup() {
        if (null != userEntivity) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    mGroupEntivities = ImGroupEntivity.find(ImGroupEntivity.class, "currentid = ?", userEntivity.getId() + "");
                    Log.i("info","mgroup=="+mGroupEntivities.size());
//                    EventBus.getDefault().post(Constant.FRAGMENT_FRIEND_REFRESH_GROUP);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mGroupAdpter.refresh(mGroupEntivities);
                            if (1 == type){
                                preTvTitle.setText(getResources().getString(R.string.group)+"（"+mGroupEntivities.size()+"）");
                            }
                        }
                    });
                }
            }).start();


        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onEventMainThread(String event) {
      /*  if (Constant.BLACK_BROADCAST_ADD_GROUP_FOUND_REFRESH.equals(event)) {
            getLoadeGroup();
        }*/
        if (Constant.FRAGMENT_FRIEND_REFRESH_GROUP.equals(event)) {
            mGroupAdpter.refresh(mGroupEntivities);
        }
    }
    @Override
    protected void onDestroy() {
//        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
